package com.mdd.front.controller.course;

import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.course.ICourseService;
import com.mdd.front.validate.*;
import com.mdd.front.vo.course.*;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 课程管理
 */
@RestController
@RequestMapping("/api/course")
public class CourseController {

    @Resource
    ICourseService iCourseService;

    /**
     * 课程列表
     *
     * @param topN 获取的个数
     * @return Object
     */
    @GetMapping("/hot/list")
    public Object listHotTopN(@RequestParam(value = "topN", defaultValue = "5") Integer topN) {
        List<CourseListVo> list = iCourseService.listHotTopN(topN);
        return AjaxResult.success(list);
    }

    /**
     * 精选课程列表
     *
     * @return Object
     */
    @GetMapping("/chosen/list")
    public Object listChosen() {
        List<CourseListVo> list = iCourseService.listChosen();
        return AjaxResult.success(list);
    }

    /**
     * 课程列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<CourseListVo> list = iCourseService.list(pageParam, params);
        return AjaxResult.success(list);
    }


    /**
     * 我的收藏列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @return Object
     */
    @GetMapping("/mine/favorites")
    public Object favorites(@Validated PageParam pageParam) {
        PageResult<CourseListVo> list = iCourseService.favorites(pageParam);
        return AjaxResult.success(list);
    }

    /**
     * 我的评论列表
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/mine/comments")
    public Object comments() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return AjaxResult.success(iCourseService.getMyComments(userId));
    }

    /**
     * 已评论的
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/mine/commented")
    public Object commented(@Validated PageParam pageParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return AjaxResult.success(iCourseService.getMyCommented(pageParam, userId));
    }

    /**
     * 根据type获取评论/未评论列表
     * @param pageParam
     * @param type
     * @return
     */
    @GetMapping("/mine/commentList")
    public Object commentList(@Validated PageParam pageParam, @RequestParam(value = "type", defaultValue = "1") int type) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return AjaxResult.success(iCourseService.getMyCommentList(pageParam, userId, type));
    }

    /**
     * 未评论
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/mine/noComment")
    public Object noComment(@Validated PageParam pageParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return AjaxResult.success(iCourseService.getMyNoComment(pageParam, userId));
    }

    /**
     * 我的评论以及未评论个数
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/mine/comment/count")
    public Object commentCount() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return AjaxResult.success(iCourseService.getMyCommentCount(userId));
    }

    @GetMapping("/mine/list")
    public Object mine() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        Map<String, List<CourseMineVo>> resultMap = iCourseService.listMine(userId);
        return AjaxResult.success(resultMap);
    }

    /**
     * 专栏课程关联列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/column/list")
    public Object listColumns(@Validated PageParam pageParam,
                       @RequestParam(value="courseId", required = true) Integer courseId,
                       @RequestParam Map<String, String> params) {
        Assert.notNull(courseId, "courseId参数缺失");
        List<CourseColumnListVo> list = iCourseService.listColumns(pageParam, params, courseId);
        return AjaxResult.success(list);
    }


    /**
     * 课程详情
     *
     * @param id 主键ID
     * @return Object
     */
    @GetMapping("/detail")
    public Object detail(@Validated @IDMust @RequestParam("id") Integer id) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        CourseDetailVo detail = iCourseService.detail(id, userId);
        return AjaxResult.success(detail);
    }

    /**
     * 课程目录详情
     *
     * @param id 主键ID
     * @return Object
     */
    @GetMapping("/catalogue/detail")
    public Object courseCatalogueDetail(@Validated @IDMust @RequestParam("id") Integer id) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        CourseCatalogueDetailVo detail = iCourseService.getCourseCatalogueDetail(id, userId);
        return AjaxResult.success(detail);
    }
    /**
     * 课程资料详情
     *
     * @param courseId 主键ID
     * @return Object
     */
    @GetMapping("/material/detail")
    public Object materialDetail(@Validated @IDMust @RequestParam("courseId") Integer courseId) {
        CourseMaterialDetailVo detail = iCourseService.getMaterialDetail(courseId);
        return AjaxResult.success(detail);
    }

    @PostMapping("/collect/switch")
    public Object collect(@Validated @RequestBody CourseCollectParam courseCollectParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return AjaxResult.success(iCourseService.collect(courseCollectParam.getId(), userId));
    }


    @PostMapping("/catalogue/study/completed")
    public Object studyCatalogue(@Validated @RequestBody StudyCourseCatalogueParam studyCourseCatalogueParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        studyCourseCatalogueParam.setUserId(userId);
        return AjaxResult.success(iCourseService.completeCourseCatalogue(studyCourseCatalogueParam));
    }

    @PostMapping("/catalogue/study/schedule/update")
    public Object updateSchedule(@Validated @RequestBody StudyCourseCatalogueScheduleParam studyCourseCatalogueScheduleParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        studyCourseCatalogueScheduleParam.setUserId(userId);
        return AjaxResult.success(iCourseService.updateCatalogueSchedule(studyCourseCatalogueScheduleParam));
    }



    @PostMapping("/study/add")
    public Object addStudyCourse(@Validated @RequestBody CourseStudyParam courseStudyParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        courseStudyParam.setUserId(userId);
        return AjaxResult.success(iCourseService.addCourseStudy(courseStudyParam.getCourseId(), userId, 0));
    }

    @PostMapping("/study")
    public Object studyCourse(@Validated @RequestBody CourseStudyParam courseStudyParam) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        courseStudyParam.setUserId(userId);
        return AjaxResult.success(iCourseService.studyCourse(courseStudyParam.getCourseId(), userId));
    }

    @GetMapping("/coupon")
    public Object listByCoupon(@Validated PageParam pageParam,
                              @RequestParam(value = "couponId", required = true) Integer couponId, @RequestParam(value = "courseName", required = false) String courseName) {

        PageResult<CourseListVo> list = this.iCourseService.listByCoupon(pageParam, couponId, courseName);


        return AjaxResult.success(list);
    }

}
